<?php

namespace modules\tencentmap;

use Throwable;
use think\facade\Cache;
use app\common\model\Config;
use app\admin\model\AdminRule;
use app\common\library\Menu;

class Tencentmap
{
    private string $uid = 'tencentmap';

    /**
     * @return void
     * @throws Throwable
     * @author jsjxsz <QQ:1365962177>
     */
    public function install(): void
    {
        $examplesMenu = AdminRule::where('name', 'examples')->value('id');
        if (!$examplesMenu) {
            $menu = [
                [
                    'type'      => 'menu_dir',
                    'title'     => '开发示例',
                    'name'      => 'examples',
                    'path'      => 'examples',
                    'icon'      => 'fa fa-code',
                    'menu_type' => 'tab',
                ]
            ];
            Menu::create($menu);
            $examplesMenu = AdminRule::where('name', 'examples')->value('id');
        }
        $menu = [
            [
                'type'      => 'menu',
                'title'     => '腾讯地图位置选择示例',
                'name'      => 'examples/tencentmap',
                'path'      => 'examples/tencentmap',
                'icon'      => 'fa fa-map-marker',
                'menu_type' => 'tab',
                'component' => '/src/views/backend/examples/tencentmap/index.vue',
                'keepalive' => '1',
                'pid'       => $examplesMenu,
                'children'  => [
                    ['type' => 'button', 'title' => '查看', 'name' => 'examples/tencentmap/index'],
                    ['type' => 'button', 'title' => '添加', 'name' => 'examples/tencentmap/add'],
                    ['type' => 'button', 'title' => '编辑', 'name' => 'examples/tencentmap/edit'],
                    ['type' => 'button', 'title' => '删除', 'name' => 'examples/tencentmap/del'],
                ],
            ]
        ];
        Menu::create($menu);
    }

    /**
     * @return void
     * @throws Throwable
     * @author jsjxsz <QQ:1365962177>
     */
    public function uninstall(): void
    {
        Menu::delete('examples/tencentmap', true);
    }

    /**
     * @return void
     * @throws Throwable
     * @author jsjxsz <QQ:1365962177>
     */
    public function enable(): void
    {
        Config::addConfigGroup('thirdpartyKey', '第三方秘钥');
        if (!Config::where('name', 'tencentmap_key')->value('id')) {
            Config::create([
                'name'      => 'tencentmap_key',
                'group'     => 'thirdpartyKey',
                'title'     => '腾讯地图KEY',
                'type'      => 'string',
                'allow_del' => 1
            ]);
        }

        // 恢复配置
        $tencentmapKey = Cache::pull($this->uid . '-module-config');
        if ($tencentmapKey) {
            Config::where('name', 'tencentmap_key')->update([
                'value' => $tencentmapKey
            ]);
        }

        Menu::enable('examples/tencentmap');
    }

    /**
     * @return void
     * @throws Throwable
     * @author jsjxsz <QQ:1365962177>
     */
    public function disable(): void
    {
        Menu::disable('examples/tencentmap');

        // 备份配置
        $tencentmapKey = Config::where('name', 'tencentmap_key')->find();
        Cache::set($this->uid . '-module-config', $tencentmapKey->value, 3600);

        $tencentmapKey->delete();
        Config::removeConfigGroup('thirdpartyKey');
    }

    /**
     * @return void
     * @author jsjxsz <QQ:1365962177>
     */
    public function update(): void
    {

    }

}